﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.Sys;
using HiStaff.Domain.LST;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using HiStaff.Domain.IN;
using HiStaff.Domain.HU;
namespace HiStaff.iInsurance
{
    public partial class ContractInsuranceType : FormBase 
    {
        private List<LST_IN_INSURANCE_TYPE> lstContType;

        public ContractInsuranceType()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit,
                CommandBarManager.ActionCommand.Separator);

            InitGrid();
            BindData();
            SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
            return base.FormLoad();
        }

        private void InitGrid()
        {
            GridEXColumn col;

            grdData.RootTable.HierarchicalMode = HierarchicalMode.SelfReferencing;
           // grdData.RootTable.SelfReferencingSettings.ChildDataMember = "PARENTID";
            grdData.RootTable.SelfReferencingSettings.ParentDataMember = "ID";
            grdData.RootTable.SelfReferencingSettings.ParentRootMode = ParentRootMode.NullValue;
            grdData.RootTable.SelfReferencingSettings.TreatOrphanRowsAsRoot = true;
            grdData.RootTable.SelfReferencingSettings.AggregationMode = SelfReferencingAggregationMode.RootRows;
            grdData.FrozenColumns = 3;

            col = new GridEXColumn();
            col.Key = "ID";
            col.DataMember = "ID";
            col.Caption = "ID";
            col.ColumnType = ColumnType.Text;
            col.EditType = EditType.NoEdit;
            col.HeaderAlignment = TextAlignment.Center;
            col.Visible = false;
            grdData.RootTable.Columns.Add(col);

            //col = new GridEXColumn();
            //col.Key = "PARENTID";
            //col.DataMember = "PARENTID";
            //col.Caption = "PARENTID";
            //col.ColumnType = ColumnType.Text;
            //col.EditType = EditType.NoEdit;
            //col.HeaderAlignment = TextAlignment.Center;
            //col.Visible = false;
            //grdData.RootTable.Columns.Add(col);

            col = new GridEXColumn();
            col.Key = "NAME";
            col.DataMember = "NAME";
            col.Caption = "TÊN HỢP ĐỒNG";
            col.ColumnType = ColumnType.Text;
            col.EditType = EditType.NoEdit;
            col.FilterEditType = FilterEditType.TextBox;
            col.HeaderAlignment = TextAlignment.Center;
            col.Width = 300;
            grdData.RootTable.Columns.Add(col);
            grdData.RootTable.SelfReferencingSettings.ExpandColumn = col;

            lstContType = OracleHelper.ExcuteSelectMultiObject<LST_IN_INSURANCE_TYPE>("APRS_LST_IN_INSURANCE_TYPE", new LST_IN_INSURANCE_TYPE { STATUS = 1 });

            foreach (var item in lstContType)
            {
                col = new GridEXColumn();
                col.Key = string.Format("A{0}", item.ID); ;
                col.DataMember = string.Format("A{0}", item.ID); ;
                col.Caption = item.NAME ;
                col.HeaderStyle.TextAlignment = TextAlignment.Center;
                col.ColumnType = ColumnType.CheckBox;
                col.EditType = EditType.CheckBox;
                col.FilterEditType = FilterEditType.SameAsEditType;
                col.CheckBoxFalseValue = 0m;
                col.CheckBoxTrueValue = 1m;
                col.CheckBoxTriState = false;
                col.Width = 120;
                col.HeaderToolTip = item.ID + " - " + item.NAME;
                col.CellToolTipText = item.ID + " - " + item.NAME;
                grdData.RootTable.Columns.Add(col);
            }
        }

        private void BindData()
        {
            grdData.DataSource = OracleHelper.ExcuteSelectDataTable("PRS_SY_CONTRACT_INS", new IN_CONTRACT_INSURANCE());
            grdData.Refresh();
        }
        protected override void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
            switch (cmd)
            {
                case CommandBarManager.ActionCommand.Edit:
                    SetEnabledCommandSave = true;
                    SetEnabledCommandCancel = true;
                    SetEnabledCommandEdit = false;
                    break;
                case CommandBarManager.ActionCommand.Cancel:
                case CommandBarManager.ActionCommand.Save:
                    SetEnabledCommandSave = false;
                    SetEnabledCommandCancel = false;
                    SetEnabledCommandEdit = true;
                    break;
            }
        }
        protected override bool EditButtonClick()
        {
            grdData.AllowEdit = InheritableBoolean.True;
            return true;
        }
        protected override bool CancelButtonClick()
        {
            grdData.AllowEdit = InheritableBoolean.False;
            BindData();
            return true;
        }
        protected override bool SaveButtonClick()
        {
            ExcuteAsynchronous();
            grdData.AllowEdit = InheritableBoolean.False;
            return true;
        }

        protected override bool AsynchronousFunctionWithBGW(System.ComponentModel.BackgroundWorker bgWorker)
        {
            DataTable dt = (DataTable)grdData.DataSource;   
            int i = dt.Rows.Count + 1;
            int j = 0;
            int percent;
            decimal?  CODE;
            foreach (DataRow drow in dt.Rows)
            {
                j++;
                CODE = ConvertHelper.ToDecimal(drow["ID"]);
                if (CODE.HasValue)
                {
                    OracleHelper.ExcuteNonQuery("APRD_IN_CONTRACT_INSURANCE", new IN_CONTRACT_INSURANCE
                    {
                        CODE_CONTRACT = CODE
                    });
                    foreach (DataColumn col in drow.Table.Columns)
                    {
                        if (drow[col.ColumnName].Equals(1m) && ConvertHelper.ToDecimal(col.ColumnName.Replace("A", "")).HasValue)
                        {
                            OracleHelper.ExcuteNonQuery("APRU_IN_CONTRACT_INSURANCE", new IN_CONTRACT_INSURANCE
                            {
                                CODE_CONTRACT = CODE,
                                IN_INSURANCE_TYPE_ID = ConvertHelper.ToDecimal(col.ColumnName.Replace("A", "")),
                            });
                        }
                    }
                }
                percent = (j * 100 / i);
                bgWorker.ReportProgress(percent, "Đang thực hiện");
            }
            bgWorker.ReportProgress(100, "Hoàn tất");
            return true;
        }
    }
}
